package com.boodaba.todocarts.ui;

import roboguice.activity.GuiceActivity;
import roboguice.inject.InjectExtra;
import roboguice.inject.InjectView;

import com.boodaba.todocarts.R;
import com.boodaba.todocarts.app.ILabelEditActivity;
import com.boodaba.todocarts.app.LabelEditPresenter;
import com.google.inject.Inject;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;

public class LabelEditActivity extends GuiceActivity implements ILabelEditActivity {
	@Inject protected LabelEditPresenter mPresenter;
	@InjectView(android.R.id.text1) protected EditText mInputString;
	@InjectView(android.R.id.button1) protected ImageButton mSaveButton;
	@InjectView(android.R.id.button2) protected ImageButton mDeleteButton;
	@InjectExtra(value="labelTitle", optional=true) protected String mTargetTitle = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_label_edit);
		mPresenter.initializeWith(this);
		
		mSaveButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mPresenter.save();
			}
		});
	}
	
	public String getTargetTitle() {
		return mTargetTitle;
	}
	
	public void setEdit(String labelTitle) {
		mInputString.setHint(labelTitle);
		mInputString.setText(labelTitle);
		
		mDeleteButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				deleteWithConfirm();
			}
		});
	}
	
	public void setNew() {
		mDeleteButton.setVisibility(View.GONE);
	}
	
	public String getInputString() {
		return mInputString.getText().toString();
	}
	
	public void exit() {
		finish();
	}
	
	private void deleteWithConfirm() {
		DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				switch (which){
					case DialogInterface.BUTTON_POSITIVE:
						mPresenter.delete();
					break;
				}
			}
		};

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder
		.setMessage(R.string.confirm_delete_label)
		.setPositiveButton(android.R.string.yes, dialogClickListener)
		.setNegativeButton(android.R.string.no, dialogClickListener)
		.show();
	}
}
